<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['status']=array('0'=>'未显示','1'=>'已显示','2'=>'已驳回');
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$params['merchid >']=0;
	$return['params']['ismerch']=$_RQ['ismerch'];
    if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['status'])){
		$params['status']=trim($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('jy_aikefu_wenda',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['msg']=cfc('tools')->substr($v['msg'],0,38);
		$v['merchname']=pdo_getcolumn('jy_merch_user',array('id'=>$v['merchid'],'uniacid'=>$_SESSION['uniacid']),'merchname');
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'问答库'),
			array('op'=>'edit','title'=>'添加问答')
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：问答优先匹配！','list'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'title','title'=>'问题','type'=>'string'),
			array('name'=>'msg','title'=>'答案','type'=>'string'),
			array('name'=>'merchname','title'=>'商户','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'问题','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'radio','options'=>'status'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_aikefu_wenda',array('id'=>$id));
	}else{
		$item=array();
	}
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'问答库',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回问答库'),
		),
		'edit'=>array(
			array('name'=>'title','title'=>'问题','type'=>'string'),
			array('name'=>'msg','title'=>'答案','type'=>'text'),
			array('name'=>'merchid','title'=>'选择商户','type'=>'choose','action'=>'jy_merch/get_merch/'),
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),		
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$postdata['latlng']=ijson_encode($postdata['latlng']);
	$data=array_fields(array('msg','status','title','merchid','paixu'),$postdata);	
	if(!$data['title']){
		exi('请填写问题','error');
	}
	if(!$data['msg']){
		exi('请填写答案','error');
	}
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_aikefu_wenda',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('设置成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_aikefu_wenda',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['status'])){
		$update['status']=intval($post['status']);
	}
	if($update){
		pdo_update('jy_aikefu_wenda',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_aikefu_wenda',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_aikefu_wenda',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
?>